package com.gaoyi.crm.vm;

import android.app.Application;
import android.database.Observable;
import android.view.Gravity;

import androidx.annotation.NonNull;
import androidx.databinding.Bindable;
import androidx.lifecycle.MutableLiveData;

import com.gaoyi.crm.R;
import com.gaoyi.crm.apiresult.ApiResultData;
import com.gaoyi.crm.callback.OnResultListener;
import com.gaoyi.crm.constant.ApiStatus;
import com.gaoyi.crm.control.EditInputView;
import com.gaoyi.crm.db.dto.ShortDto;
import com.gaoyi.crm.db.entity.ItemNoStore;
import com.gaoyi.crm.db.entity.Pmn;
import com.gaoyi.crm.db.entity.PmnAttach;
import com.gaoyi.crm.db.entity.PmnWithAttach;
import com.gaoyi.crm.repository.ItemNoStoreRepository;
import com.gaoyi.crm.repository.NewPmnRepository;
import com.gaoyi.crm.repository.PmnRepository;
import com.gaoyi.crm.util.DensityUtil;
import com.gaoyi.crm.util.LanguageUtils;
import com.gaoyi.crm.util.StringUtil;
import com.gaoyi.crm.util.ToastUtil;
import com.gaoyi.crm.vm.base.BaseViewModel;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

public class NewPromotionViewModel extends BaseViewModel {

    final ItemNoStoreRepository mItemNoStore;
    final PmnRepository mPmn;
    final NewPmnRepository mNewPmn;
    MutableLiveData<List<ShortDto>> mCusList;
    MutableLiveData<List<PmnAttach>> mPhotoList;
    MutableLiveData<Boolean> mIsExist;
    MutableLiveData<Integer> mResultCode;
    MutableLiveData<Integer> mUpdateCode;
    MutableLiveData<Pmn> mPmnEdit;
    MutableLiveData<String> mProId;
    MutableLiveData<String> mProEnName;
    MutableLiveData<String> mCusName;
    MutableLiveData<String> mCusId;
    MutableLiveData<String> mRemark;
    MutableLiveData<String> mDescip;
    ArrayList<ItemNoStore> mScanList;

    public MutableLiveData<String> getRemark() {
        if (mRemark == null) {
            mRemark = new MutableLiveData<>();
        }
        return mRemark;
    }

    public MutableLiveData<String> getCusName() {
        if (mCusName == null) {
            mCusName = new MutableLiveData<>();
        }
        return mCusName;
    }

    public MutableLiveData<String> getCusId() {
        if (mCusId == null) {
            mCusId = new MutableLiveData<>();
        }
        return mCusId;
    }

    public MutableLiveData<String> getProEnName() {
        if (mProEnName == null) {
            mProEnName = new MutableLiveData<>();
        }
        return mProEnName;
    }

    public MutableLiveData<String> getProId() {
        if (mProId == null) {
            mProId = new MutableLiveData<>();
        }
        return mProId;
    }

    public MutableLiveData<String> getDescip() {
        if (mDescip == null) {
            mDescip = new MutableLiveData<>();
        }
        return mDescip;
    }

    public ArrayList<ItemNoStore> getScanList() {
        return mScanList;
    }

    public MutableLiveData<List<ShortDto>> getCusList() {
        if (mCusList == null) {
            mCusList = new MutableLiveData<>();
        }
        return mCusList;
    }

    public void setIsExist(Boolean isExist){
        mIsExist.setValue(isExist);
    }

    public MutableLiveData<Boolean> getIsExist() {
        if (mIsExist == null) {
            mIsExist = new MutableLiveData<>();
        }
        return mIsExist;
    }

    public MutableLiveData<Integer> getResultCode() {
        if (mResultCode == null) {
            mResultCode = new MutableLiveData<>();
        }
        return mResultCode;
    }

    public MutableLiveData<Integer> getUpdateCode() {
        if (mUpdateCode == null) {
            mUpdateCode = new MutableLiveData<>();
        }
        return mUpdateCode;
    }

    public MutableLiveData<Pmn> getPmnEdit() {
        if (mPmnEdit == null) {
            mPmnEdit = new MutableLiveData<>();
        }
        return mPmnEdit;
    }

    public MutableLiveData<List<PmnAttach>> getPhotoList() {
        if (mPhotoList == null) {
            mPhotoList = new MutableLiveData<>();
            mPhotoList.setValue(new ArrayList<>());
        }
        return mPhotoList;
    }

    public void addPhoto(PmnAttach bean) {
        List<PmnAttach> list = new ArrayList<>(mPhotoList.getValue());
        list.add(bean);
        mPhotoList.setValue(list);
    }

    public void removePhoto(int position) {
        List<PmnAttach> list = new ArrayList<>(mPhotoList.getValue());
        list.remove(position);
        mPhotoList.setValue(list);
    }

    public void clearPhoto() {
        mPhotoList.setValue(new ArrayList<>());
    }

    public NewPromotionViewModel(@NonNull Application application) {
        super(application);
        mItemNoStore = new ItemNoStoreRepository();
        mPmn = new PmnRepository();
        mNewPmn = new NewPmnRepository();
        mScanList = new ArrayList<>();
    }

    public void getCustomers() {
        if (getView() != null) {
            getView().showLoading();
        }
        mApplication.getContainer().getCombo().combo("customers", new OnResultListener() {
            @Override
            public void onSuccess(Object apiResult) {
                if (getView() != null) {
                    getView().hideLoading();
                }
                ApiResultData<List<ShortDto>> apiResultData = (ApiResultData<List<ShortDto>>) apiResult;
                if (apiResultData.getStatus() != ApiStatus.SUCCESS) {
                    ToastUtil.showToast(apiResultData.getMessage());
                    return;
                }
                mCusList.setValue(apiResultData.getData());
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().hideLoading();
                }
                ToastUtil.showToast(R.string.network_error);
            }
        }, new TypeToken<ApiResultData<List<ShortDto>>>() {
        }.getType());
    }

    public void getItemNo(String itemNo) {
        mItemNoStore.getItemNo(itemNo, new OnResultListener() {
            @Override
            public void onSuccess(Object aipResult) {
                ApiResultData<ItemNoStore> apiResultData = (ApiResultData<ItemNoStore>) aipResult;
                if (apiResultData.getStatus() != ApiStatus.SUCCESS) {
                    ToastUtil.showToast(StringUtil.getString(R.string.item_no_no_record, itemNo));
                    return;
                }
                ItemNoStore data = apiResultData.getData();
                if (data == null) {
                    ToastUtil.showToast(StringUtil.getString(R.string.item_no_no_record, itemNo));
                    return;
                }
                // 判断itemNo是否已存在
                for (ItemNoStore itemNoStore : mScanList) {
                    if (itemNoStore.getItemNo().equals(itemNo)) {
                        ToastUtil.showToast(R.string.same_item_no);
                        return;
                    }
                }
                ToastUtil.showToast(StringUtil.getString(R.string.item_no_scan_success, itemNo));
                mScanList.add(data);
            }

            @Override
            public void onFails(Exception e) {
                ToastUtil.showToast(R.string.network_error);
            }
        }, new TypeToken<ApiResultData<ItemNoStore>>() {
        }.getType());
    }

    public void getPmn(String id) {
        mPmn.getPmnWithListById(id, new OnResultListener() {
            @Override
            public void onSuccess(Object aipResult) {
                ApiResultData<PmnWithAttach> apiResultData = (ApiResultData<PmnWithAttach>) aipResult;
                if (apiResultData.getStatus() != ApiStatus.SUCCESS) {
                    ToastUtil.showToast(apiResultData.getMessage());
                    mResultCode.setValue(apiResultData.getStatus());
                    return;
                }
                PmnWithAttach pmnWithAttach = apiResultData.getData();
                Pmn pmn = pmnWithAttach.pmn;
                getPmnEdit().setValue(pmn);
                getProId().setValue(pmn.getCode());
                getProEnName().setValue(pmn.getEnName());
                getCusId().setValue(pmn.getCustomerId());
                getCusName().setValue(LanguageUtils.getInstance().isEnglish() ? pmn.getCustomerEnName() : pmn.getCustomerCnName());
                getDescip().setValue(pmn.getDescription());
                getRemark().setValue(pmn.getRemark());
                List<PmnAttach> pmnAttaches = pmnWithAttach.pmnAttachs;
                if (pmnAttaches != null && !pmnAttaches.isEmpty()) {
                    getPhotoList().setValue(pmnAttaches);
                }
            }

            @Override
            public void onFails(Exception e) {
                ToastUtil.showToast(R.string.network_error);
                mResultCode.setValue(ApiStatus.FAIL);
            }
        });
    }

    public void codeIsExist(String code) {
        mPmn.getPmnByCodeIsExist(code, new OnResultListener() {
            @Override
            public void onSuccess(Object aipResult) {
                ApiResultData<Boolean> apiResultData = (ApiResultData<Boolean>) aipResult;
                Boolean data = apiResultData.getData();
                mIsExist.setValue(data);
            }

            @Override
            public void onFails(Exception e) {
            }
        });
    }

    public void updatePmn(Pmn pmn) {
        if (getView() != null) {
            getView().showLoading();
        }
        mNewPmn.updatePmn(pmn, new OnResultListener() {
            @Override
            public void onSuccess(Object aipResult) {
                if (getView() != null) {
                    getView().hideLoading();
                }
                ApiResultData<String> apiResultData = (ApiResultData<String>) aipResult;
                ToastUtil.showToast(apiResultData.getMessage());
                mUpdateCode.setValue(apiResultData.getStatus());
            }

            @Override
            public void onFails(Exception e) {
                if (getView() != null) {
                    getView().hideLoading();
                }
                ToastUtil.showToast(R.string.network_error);
                mUpdateCode.setValue(ApiStatus.FAIL);
            }
        });
    }
}
